from flask_wtf import FlaskForm
from flask_ckeditor import CKEditorField
from wtforms import StringField, PasswordField, TextAreaField, SubmitField, SelectField, RadioField, HiddenField, FileField
from wtforms.validators import DataRequired, EqualTo
from app.models import Category


class LoginForm(FlaskForm):
    username = StringField(
        label='用户名',
        validators=[
            DataRequired(message='用户名为空')
        ],
        render_kw={
            "class": "form-control",
            "placeholder": "用户名"
        }
    )
    password = PasswordField(
        label='密码',
        validators=[
            DataRequired(message='密码不能为空')
        ],
        render_kw={
            "class": "form-control",
            "placeholder": "密码"
        }
    )
    # password2 = PasswordField(label='确认密码', validators=[
    #     EqualTo('password', message='两次密码不一样')
    # ])
    submit = SubmitField(
        label='登 录',
        render_kw={
            "class": "btn btn-lg btn-dark btn-block"
        }
    )


class CategoryForm(FlaskForm):
    name = StringField(
        label="文章分类",
        validators=[
            DataRequired("请输入文章分类")
        ],
        description="文章分类",
        render_kw={
            "id": "name",
            "class": "form-control"
        }
    )
    status = RadioField(
        label="是否启用",
        coerce=int,
        choices=[(1, "【√ 是】"), (0, "【× 否】")],
        default=1,
        description="是否启用",
        render_kw={
            "id": "status",
            "class": "form-check-input"
        }
    )
    submit = SubmitField(
        '提交表单',
        render_kw={
            "class": "btn btn-lg btn-dark"
        }
    )


class PostForm(FlaskForm):
    title = StringField(
        label="文章标题",
        validators=[
            DataRequired("请输入文章标题!")
        ],
        description="文章标题",
        render_kw={
            "id": "title",
            "class": "form-control"
        }
    )
    cover = StringField(
        label="文章封面图",
        description="文章封面图",
        validators=[
            # DataRequired("必须上传封面图!")
        ],
        render_kw={
            "class": "form-control"
        }
    )
    desc = TextAreaField(
        label="文章概述",
        validators=[
            DataRequired("请输入文章概述!")
        ],
        description="文章概述",
        render_kw={
            "rows": 3,
            "id": "desc",
            "class": "form-control"
        }
    )
    category = SelectField(
        label="文章类别",
        validators=[
            DataRequired("请输入文章类别!")
        ],
        coerce=int,
        # 这个可以从数据库实时获取
        # choices=[(1, "Python"), (2, "PHP"), (3, "Go"), (4, "Vue")],
        choices=[],
        # choices=[],
        description="文章类别",
        render_kw={
            "id": "category",
            "class": "form-control"
        }
    )
    top = RadioField(
        label="是否置顶",
        coerce=int,
        choices=[(1, "【√ 是】"), (0, "【× 否】")],
        default=1,
        description="是否置顶",
        render_kw={
            "id": "top",
            "class": "form-check-input"
        }
    )
    body = CKEditorField(
        label="文章内容",
        validators=[
            DataRequired("请输入文章内容!")
        ],
        description="文章内容",
        render_kw={
            "id": "body",
            "class": "form-control"
        }
    )
    submit = SubmitField(
        '提交表单',
        render_kw={
            "class": "btn btn-lg btn-dark"
        }
    )

    def __init__(self, *args, **kwargs):
        super(PostForm, self).__init__(*args, **kwargs)
        self.category.choices = [(v.id, v.name) for v in Category.query.order_by(Category.name).all()]


class AdminForm(FlaskForm):
    pass
